package edu.aynu.service.impl;

import edu.aynu.service.EmailService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: 清视°
 * Date: 2021/6/21  10:26
 * Description: 1、邮箱的发送
 * “          ” 2、验证码的储存
 */
@Service
public class EmailServiceImpl implements EmailService {

    /**
     * 和redis的操作
     */
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 邮箱操作
     */
    @Resource
    JavaMailSenderImpl mailSender;


    @Override
    public Boolean sendEmail(String email,String code) {
        if (redisTemplate.hasKey(email)){
            return false;
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("涛帅考试科技");//头部
        message.setText("【涛帅考试】 验证码："+code+"。验证码有效时间为5分钟。您正在使用注册功能，验证码提供他人可能导致账号被盗，请勿转发或泄露。账户初始密码为：123");//内容
        message.setTo(email);//接收方邮箱
        message.setFrom("2081957565@qq.com");//发送方邮箱
        mailSender.send(message);     //邮箱发送
        redisTemplate.opsForValue().set(email,code,60*5, TimeUnit.SECONDS);  //验证码放入redis
        return true;
    }

    @Override
    public Object getCodeRedis(String email) {
        return redisTemplate.opsForValue().get(email);
    }
}
